package com.audaque.springboot.foshanupload.authcore.model.currentUser;

import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.Serializable;
import java.util.Date;

public class OptContext implements Comparable<OptContext>, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 这里默认时间值 - 0
     */
    public static final OptContext ROBOT = new OptContext(new Date(0));

    private int userId = 0;// 当前操作用户ID
    private String account;// 登录账号
    private String name;// 用户姓名
    private int tenantId = 0;// 租户ID
    private Long optTime;// 当前操作的时间

    public OptContext() {
        this.optTime = System.currentTimeMillis();
    }

    public OptContext(String str) {
        // todo why？
        this.optTime = System.currentTimeMillis();
    }

    public OptContext(Date optTime) {
        this.optTime = optTime.getTime();
    }

    public OptContext(int tenantId) {
        this.tenantId = tenantId;
        this.optTime = System.currentTimeMillis();
    }

    public OptContext(int userId, int tenantId) {
        this.userId = userId;
        this.tenantId = tenantId;
        this.optTime = System.currentTimeMillis();
    }

    public OptContext(int userId, int tenantId, Date optTime) {
        this.userId = userId;
        this.tenantId = tenantId;
        this.optTime = optTime.getTime();
    }

    public OptContext(int userId, String account, String name, int tenantId) {
        this.userId = userId;
        this.account = account;
        this.name = name;
        this.tenantId = tenantId;
        this.optTime = System.currentTimeMillis();
    }

    public OptContext(int userId, String account, String name, int tenantId, Date optTime) {
        this.userId = userId;
        this.account = account;
        this.name = name;
        this.tenantId = tenantId;
        this.optTime = optTime.getTime();
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTenantId() {
        return tenantId;
    }

    public void setTenantId(int tenantId) {
        this.tenantId = tenantId;
    }

    public Date getOptTime() {
        if (optTime == null) {
            return null;
        }
        return new Date(optTime);
    }

    public void setOptTime(Date optTime) {
        if (optTime == null) {
            this.optTime = null;
            return;
        }
        this.optTime = optTime.getTime();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        OptContext newObj = (OptContext) obj;

        EqualsBuilder builder = new EqualsBuilder();
        builder.append(getUserId(), newObj.getUserId());
        return builder.isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(getUserId());
        return builder.toHashCode();
    }

    @Override
    public int compareTo(OptContext o) {
        if (o == this) {
            return 0;
        }

        CompareToBuilder builder = new CompareToBuilder();
        builder.append(getUserId(), o.getUserId());
        return builder.toComparison();
    }

}
